<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="../../common/includeBaseSet.jsp" %>
<%@include file="../../common/operation_forbid.jsp" %>
<%@include file="../../common/jsVersion.jsp"%>
<script type="text/javascript">
    var contextPath = '${jypath}';
    var documentType=${documentType};
    var returnUrl = '${returnUrl}';
	var haveReceivedPayDepositAmount="${payDeposit.payDepositAmount}";
	if(haveReceivedPayDepositAmount==""){
		haveReceivedPayDepositAmount=0;
	}
	var payDepositCode="${payDeposit.depositCode}";
	var projectId="${projectId}";
	var agentId="${payDeposit.agentId}";
	var currentUserName='';
    var customerId = '${customerId}';
    var roomId = '${roomId}';
    var closeTabRoomId = roomId;
    var userId = '${userId}';
    var payDepositId = '${payDeposit.id}';
    var menu= '${menu}';
    var customerProjectId='${customerProjectId}';
    var payDepositOff=${payDepositOff};
    var showChooseCustomer=${showChooseCustomer};
    var token = '${token}';
    var url = '${url}';
    var conversionSign = 7;
</script>
<!DOCTYPE html >
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>认购</title>
    <!-- neptune -->
    <%@include file="../../common/extInclude.jsp" %>
    <script type="text/javascript" src="${contextPath}/static/js/jquery/json2.js"></script> 
    <script type="text/javascript" src="${contextPath}/static/js/components/customerComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/components/estateComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/components/userComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/customer/subscribe/add.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/components/fileComponent.js?v=${jsVersion}"></script>
</head>
<script type="text/javascript">
var userIdStore = null;
var addPanel=null;
Ext.onReady(function() {
    Ext.Ajax.timeout = 180000; 
    Ext.define('Record', { 
        extend: 'Ext.data.Model',
        fields: ["relationAgentid","proportionCommission"]
    }); 
	userIdStore = new Ext.data.Store({
    	        fields: ["userName", "id"],
    	        proxy:{
    	            type:'ajax',
    	            url:contextPath+'/customer/customer/userByProjectIdForGroup',
    	            reader:{
                        type: 'json', 
                        root: 'root'  //数据
                   }
    	        },
    			autoLoad:false
    });
	Ext.tip.QuickTipManager.init()
	addPanel=Ext.create("customer.subscribe.addPanel");
	if(roomId!="" && null!=roomId){
		findRoomInfo(roomId,null);
		//当房间ID不为空时隐藏选择房源按钮
		Ext.getCmp("changeHouse").setVisible(false);
	}
	if(customerId!="" && null!=customerId){

		findCustomer(customerId);

	}
	if(payDepositId!="" && null!=payDepositId){
		//设置认筹信息
		var cId = '${payDeposit.customerId}';
		var rId = '${payDeposit.roomId}';
		findRoomInfo(rId,payDepositId);
		findCustomer(cId);
		
		//认筹转认购，隐藏选择房间按钮,如果是无房认筹,就可以选择
		var isPaydeposit = '${payDeposit.isPaydeposit}';
		if(isPaydeposit==0){
			 Ext.getCmp("changeHouse").setVisible(false);
		} 
	}
});


//加载房间信息  , 直接认购， 从价格方案里面获取信息

function findRoomInfo(roomId,paydepositId){
	var reqUrl = null;
	if(paydepositId!=null){
		reqUrl=contextPath+"/estate/projectRoom/findPayDeposit?roomId="+roomId+"&paydepositId="+paydepositId;
	}else{
		reqUrl=contextPath+"/estate/projectRoom/roomDetail?id="+roomId;
	}
	//初始化房间信息
 	 Ext.Ajax.request({
 	    url: reqUrl,
 	    method: 'POST',
 	    success: function(response){
 	        var text = Ext.decode(response.responseText);
 	        if(text.res=="1"){
     			addPanel.getComponent("estatePanel").getForm().setValues(text.obj);
      	     	//设置折扣信息
      	    	var result=text.obj;
        	  	var subscribeInfoForm=Ext.getCmp("subscribeInfoForm").getForm();
        	  	if(result.floorType=="1"){
        	  		result.discountAfterAmount = 0;
  	  				result.discountRateDisplay="注：折扣大于等于"+(100-result.discountRate)+"%将发起购房折扣审批，且审批完成后才生效";
  	        	 	
	       			//隐藏折后总价、显示折扣比例
        	  	 	Ext.getCmp('discountAfterPrice').setVisible(false);
        	  	 	Ext.getCmp("discountRate").setVisible(true);
        	  	 	subscribeInfoForm.findField("discountAfterAmount").setReadOnly(false);
        	  	}else if(result.floorType=="0"){
        	  		result.discountAfterAmount=0;
        	 	    result.discountRateDisplay="注：折后总价低于"+result.discountRate+"元将发起购房折扣审批，且审批完成后才生效";
        	 	 	//显示折后总价、隐藏折扣比例
        	 	    Ext.getCmp('discountAfterPrice').setVisible(true);
        	 	    Ext.getCmp("discountRate").setVisible(false);
        	 	    subscribeInfoForm.findField("discountAfterAmount").setReadOnly(true);
        	  	}
        	    result.discountRateHidden=result.discountRate;
 	        	subscribeInfoForm.findField("isCalculate").setValue("1"); 
 	        	subscribeInfoForm.findField("remark").setValue(""); 
      	    	subscribeInfoForm.setValues(result); 
 	        }
 	    }
 	});
}

function findCustomer(customerId){
	
	//初始化房间信息
 	 Ext.Ajax.request({
	    url: contextPath+'/customer/customer/customerDetail',
	    params: {
	    	id: customerId
	    },
	    method: 'POST',
	    success: function(response){
	        var text = Ext.decode(response.responseText);
	        if(text.res=="1"){

	        	 //初始化客户信息
	     		  addPanel.getComponent("customerPanel").getForm().setValues(text.obj);
	     		  var familyMemberGridStore=addPanel.getComponent("customerPanel").getComponent("familyMemberGridPanel").getStore();
	     		 var extraParams=familyMemberGridStore.getProxy().extraParams;
	     		  if(payDepositId!=""){
	     			  //当认筹ID不为空则加载认筹的客户成员信息
	     			  extraParams.buyId=payDepositId;
	     			  extraParams.memberType="2";
		 	          familyMemberGridStore.load();
		 	          userIdStore.load({
  		 	        	  params:{
   		 	        		  'projectId':text.obj.projectId
   		 	        	  },
 		 	        	  	callback:function(){
   		 	        	  		var index = userIdStore.find('id',userId);  
		                        if(index!=-1){  
		                            var record  = new Record({
		                            	relationAgentid:userId,
		                            	proportionCommission:'100'
		                            }); 
		                            gridStoreJingJ.insert(0, record);
		                        }
   		 	        	  	}
   		 	          });
   		 	          //Ext.getCmp('userName').setValue(agentId);
	     		  }else{
  		 	         //Ext.getCmp('userName').setValue(text.obj.userId);
   		 	         userIdStore.load({
  		 	        	  params:{
   		 	        		  'projectId':text.obj.projectId
   		 	        	  },
 		 	        	  	callback:function(){
   		 	        	  		var index = userIdStore.find('id',userId);  
		                        if(index!=-1){  
		                            var record  = new Record({
		                            	relationAgentid:userId,
		                            	proportionCommission:'100'
		                            }); 
		                            gridStoreJingJ.insert(0, record);
		                        }
   		 	        	  	}
   		 	         });
					  extraParams.customerId=customerId;
					  //如果是充客户转过来，则默认展示客户的家庭成员
					  extraParams.memberType="1";
	     		  }
// 		 	      familyMemberGridStore.on("beforeload",function(){
// 		 	    	  Ext.apply(familyMemberGridStore.proxy.extraParams,{customerId:customerId});
//     	     	  });
		 	      familyMemberGridStore.load();  

		 	          
		 	          
	        }
	    }
	});
}
</script>
<body>
</body>
</html>